﻿/**
 * RichTextFieldTest
 * @author alex
 * 
 * modified by sban <http://blog.sban.com.cn/>
 * 2009/8/23
 */

package com.riaidea.richtextfield
{
	import flash.display.*;
	import flash.events.*;		
	import flash.text.*;
	import flash.utils.*;
	import flash.ui.*;
	import fl.controls.*;
	
	import com.riaidea.richtextfield.RichTextField;
	
	[SWF(width=500, height=450, frameRate=24, backgroundColor=0x333333)]
	public class RichTextFieldTest extends Sprite 
	{
		private var _output:RichTextField;
		private var _input:RichTextField;
		
		private var _sendBtn:Button;
		private var _smileyContainer:Sprite;		
		private var _outputScrollBar:UIScrollBar;
		private var _inputScrollBar:UIScrollBar;
		
		//smileys
		private var _smileys:Array = [jAcid_smiley, jCool_smiley, 
									  jEyelash_smiley, jGawp_smiley, jGrin_smiley, 
									  jHmm_smiley, jHuh_smiley, jKiss_smiley, 
									  jLaugh_smiley, jSad_smiley, jShocked_smiley, 
									  jUnhappy_smiley, jWhat_smiley, jWink_smiley, 
									  jXiaoYu_smiley];
		
		
		public function RichTextFieldTest():void 
		{
			if (stage != null && stage.stageWidth > 0) init();
			else addEventListener(Event.ENTER_FRAME, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ENTER_FRAME, init);
			
			stage.scaleMode = "noScale";
			stage.align = "topLeft";
			
			//text format
			var titleFormat:TextFormat = new TextFormat("Arial", 12, 0x000000, true);	
			var txtFormat:TextFormat = new TextFormat("Courier New", 12, 0x000000, false);	
			
			//output title
			var output_title:TextField = new TextField();
			output_title.defaultTextFormat = titleFormat;
			output_title.selectable = false;
			output_title.text = "输出:";
			output_title.x = 10;
			output_title.y = 10;			
			addChild(output_title);			
			
			//output border
			var outputBorder:Shape = new Shape();
			outputBorder.graphics.lineStyle(1, 0x666666);
			outputBorder.graphics.drawRect(10, 30, 476, 200);
			addChild(outputBorder);
			
			//output richtextfield
			_output = new RichTextField(460, 200);
			_output.x = 10;
			_output.y = 30;
			_output.defaultTextFormat = txtFormat;			
			_output.lineHeight = 22;
			_output.autoScroll = true;
			addChild(_output);
			
			//output scrollbar
			_outputScrollBar = getUIScrollBar();			
			_outputScrollBar.scrollTarget = _output.textfield;
			_outputScrollBar.x = _output.x + _output.width;
			_outputScrollBar.y = _output.y;
			_outputScrollBar.height = _output.height;	
			addChild(_outputScrollBar);	
			
			//input title
			var input_title:TextField = new TextField();				
			input_title.defaultTextFormat = titleFormat;
			input_title.selectable = false;
			input_title.text = "输入:";
			input_title.x = 10;
			input_title.y = 240;	
			addChild(input_title);
			
			//output border
			var inputBorder:Shape = new Shape();
			inputBorder.graphics.lineStyle(1, 0x666666);
			inputBorder.graphics.drawRect(10, 260, 476, 100);
			addChild(inputBorder);
			
			//input richtextfield			
			_input = new RichTextField(460, 100, RichTextField.INPUT);			
			_input.x = 10;
			_input.y = 260;	
			_input.textfield.multiline = false;
			_input.placeholderColor = 0xFFFFFF;
			_input.defaultTextFormat = txtFormat;
			_input.spriteHspace = _input.spriteVspace = 2;
			addChild(_input);
			
			//input scrollbar
			_inputScrollBar = getUIScrollBar();
			_inputScrollBar.scrollTarget = _input.textfield;
			_inputScrollBar.x = _input.x + _input.width;
			_inputScrollBar.y = _input.y;
			_inputScrollBar.height = _input.height;
			addChild(_inputScrollBar);
			
			//add smileys
			_smileyContainer = new Sprite();
			addChild(_smileyContainer);
			_smileyContainer.x = 25;
			_smileyContainer.y = 375;
			createSmileys();
			
			//send button
			_sendBtn = getButton();
			addChild(_sendBtn);
			_sendBtn.x = 360;
			_sendBtn.y = 390;
			_sendBtn.label = "发送";
			_sendBtn.addEventListener(MouseEvent.CLICK, sendMessage);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.focus = _input.textfield;
			
			//*/add some sample text
			_output.appendRichText("UserA: 支持图文混编的文本框，有吗？", [ { src:_smileys[0], index:-1 } ]);
			_output.appendRichText("UserB: 呵呵，当然有，RichTextField就可以。", [ { src:_smileys[4], index:9 } ]);
			_output.appendRichText("UserA: 那它有什么功能呢？", [ { src:_smileys[3], index:10 } ]);
			_output.appendRichText("UserB: 可以在文本任意位置插入表情图片，就像QQ聊天一样。", [ { src:_smileys[1], index:7 }, { src:_smileys[2], index:22 } ]);
			_output.appendRichText("UserB: 也可以自由输入、编辑你的文本和表情图片。", [ { src:_smileys[5], index:-1 } ]);
			_output.appendRichText("UserB: 还不赶快在下面的输入框里试试看！", [ { src:_smileys[7], index:18 } ]);
			//*/
		}
		
		private function onKeyDown(evt:KeyboardEvent):void
		{			
			if (evt.keyCode == Keyboard.ENTER && _input.length > 0)
			{				
				sendMessage();
			}
		}
		
		private function sendMessage(evt:MouseEvent = null):void
		{
			_output.appendFromRichText(_input);
			if (_output.textfield.maxScrollV > 1)_output.textfield.scrollV = _output.textfield.maxScrollV;
			_input.clear();
			_outputScrollBar.update();
		}
		
		private function insertSmiley(evt:MouseEvent):void
		{
			var smiley:Sprite = evt.currentTarget as Sprite;
			var smileyClass:Class = getDefinitionByName(getQualifiedClassName(smiley)) as Class;
			_input.addSprite(smileyClass);
			
			//set focus/caretIndex to _input
			stage.focus = _input.textfield;
			if (_input.isSprite(_input.textfield.caretIndex))
			{
				_input.setCaretIndex(_input.textfield.caretIndex + 1);
			}
		}
		
		private function createSmileys():void
		{
			for (var i:int = 0; i < _smileys.length; i++)
			{
				var smiley:Sprite = new _smileys[i]() as Sprite;
				_smileyContainer.addChild(smiley);
				smiley.x = (i % 8) * 35;
				smiley.y = Math.floor(i / 8) * 30;
				smiley.buttonMode = true;
				smiley.addEventListener(MouseEvent.CLICK, insertSmiley);			
			}
		}
		
		private function getButton():Button
		{
			var btn:Button = new Butto();
			/*btn.setStyle("upSkin", Button_upSkin);
			btn.setStyle("overSkin", Button_overSkin);
			btn.setStyle("downSkin", Button_downSkin);
			btn.setStyle("disabledSkin", Button_disabledSkin);
			btn.setStyle("textFormat", new TextFormat("Arial", 12, 0x000000));*/
			return btn;
		}
		
//		private function getUIScrollBar():UIScrollBar
//		{
//			var scrollBar:UIScrollBar = new UIScrollBar();
//			/*scrollBar.setStyle("trackUpSkin", ScrollBars_trackSkin);
//			scrollBar.setStyle("trackOverSkin", ScrollBars_trackSkin);
//			scrollBar.setStyle("trackDownSkin", ScrollBars_trackSkin);
//			scrollBar.setStyle("trackDisabledSkin", ScrollBars_trackSkin);			
//			scrollBar.setStyle("thumbUpSkin", ScrollBars_thumbUpSkin);
//			scrollBar.setStyle("thumbOverSkin", ScrollBars_thumbOverSkin);
//			scrollBar.setStyle("thumbDownSkin", ScrollBars_thumbDownSkin);
//			scrollBar.setStyle("thumbIcon", ScrollBars_thumbIcon);			
//			scrollBar.setStyle("downArrowUpSkin", ScrollBars_downArrowUpSkin);
//			scrollBar.setStyle("downArrowOverSkin", ScrollBars_downArrowOverSkin);
//			scrollBar.setStyle("downArrowDownSkin", ScrollBars_downArrowDownSkin);
//			scrollBar.setStyle("downArrowDisabledSkin", ScrollBars_downArrowDisabledSkin);
//			scrollBar.setStyle("upArrowUpSkin", ScrollBars_upArrowUpSkin);
//			scrollBar.setStyle("upArrowOverSkin", ScrollBars_upArrowOverSkin);
//			scrollBar.setStyle("upArrowDownSkin", ScrollBars_upArrowDownSkin);
//			scrollBar.setStyle("upArrowDisabledSkin", ScrollBars_upArrowDisabledSkin);*/			
//			return scrollBar;
//		}
		
	}	
}